<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 


$(function(){
     var $orgSelect = $('#orgs');
     var $orgExtSelect = $('#orgExt');
     var $orgCategorySelect = $('#orgCategory');
     var $parentOrgSelect = $('#parentOrg');
     
     //给$orgExtSelect绑定chage事件
     $orgExtSelect.change(function(){
         var $option = $(this).find('option:selected'); //当前option
         if($option.length == 1){
             var oc = $option.attr('orgCategory'); //类别
             var po = $option.attr('parentOrg');    //父组织
             showCategoryAndOrg(oc,po);
         }else{
             resetOption($orgCategorySelect);
             resetOption($parentOrgSelect);
         }
     });    
});

/**
重置select
**/
function resetOption($select){
	$select.find("option[value='']").attr("selected","selected");
}

/**
显示组织类别和父组织
**/
function showCategoryAndOrg(category,org){
    var $orgCategorySelect = $('#orgCategory');
    var $parentOrgSelect = $('#parentOrg');
	//显示类别
	$orgCategorySelect.find('option').each(function(){
	  var $currectCategory = $(this);
	  if($currectCategory.val() == category){
	      //$currectCategory.attr("selected","selected");
		  $('#orgCategory').combobox('select',category);
	  }
	})
	//显示组织
	$parentOrgSelect.find('option').each(function(){
	  var $currectOrg = $(this);
	  if($currectOrg.val() == org){
		  $('#parentOrg').combobox('select',org);
	      //$currectOrg.attr("selected","selected");
	  }
	})
}

//添加选择项
function addItems(){
	var $orgSelect = $('#orgs');
    if(checkSelectOption($orgSelect)){
        submitFormOnType('addOrg'); //保存组织
    }	    
}

/*
删除选择项
*/
function deleteItems(){
	var $orgExtSelect = $('#orgExt');
	if(checkSelectOption($orgExtSelect)){
	    submitFormOnType('delOrg'); //删除组织
	}
}

/**
更新组织参数
**/
function updateOrgParam(){
	var $orgExtSelect = $('#orgExt');
	if(checkSelectOption($orgExtSelect) && checkParam()){
	    submitFormOnType('updateOrgParam'); //更新组织参数
	}
}

/**
验证是否选择参数
**/
function checkParam(){
	var flag = true;
	var $orgCategorySelect = $('#orgCategory');
    var $parentOrgSelect = $('#parentOrg');
	if($orgCategorySelect.find('option:selected').val()==''
	|| $parentOrgSelect.find('option:selected').val()==''){
		$.messager.alert("提示",'请选择类别或组织!');
	    flag = false;
	}
	return flag;
}

//验证选中个数
function checkSelectOption($select){
    var flag = true;
    if($select.find('option:selected').length == 0){
    	$.messager.alert("提示",'请选择项目!');
        flag = false;
    }
    return flag;
}

/*
提交表单
*/    
function submitFormOnType(type){
    $('form').find('#typeParam').val(type);
    $('form').submit();
}